/**
 * =====================================================================================
 *       @file  ngx_recv.c
 *      @brief  
 *
 *  Detailed description starts here.
 *
 *   @internal
 *     Created  2012年05月15日 13时39分36秒 
 *    Revision  1.0.0.0
 *    Compiler  gcc/g++
 *
 *     @author  luis
 * =====================================================================================
 */
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "ngx_event.h"
#include "ngx_core.h"
#include "ngx_connection.h"

ssize_t ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
{
    ssize_t             n;       
    int                 err;
    ngx_event_t        *rev;

    rev = c->read;

    do 
    {
        n = recv(c->fd, buf, size, 0);         
        printf("recv: fd:%d %d of %d", c->fd, n , size);

        if (n == 0)
        {
            rev->ready = 0; 
            rev->eof = 1;
            return n;
        }
        else if (n > 0)
        {
            if ((size_t)n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT))
            {
                rev->ready = 0;
            }
            return n;
        }

        err = errno;
        if (err == EAGAIN || err == EINTR)
        {
            printf("recv() not ready"); 
            n = NGX_AGAIN;
        }
        else
        {
            n = NGX_ERROR;
            break;            
        }

    } while (err == EINTR);

    rev->ready = 0;

    if (n == NGX_ERROR)
    {
        rev->error = 1; 
    }

    return n;
}

